import {getTables} from "./../db/table";
import {getFidlds} from "./../db/fields";
import fieldGet from "./fieldGet";
export default  async function initTable(){
    const tables = [];
    let rsTable = await getTables();   
    for (let index = 0; index < rsTable.length; index++) {
        const element = rsTable[index];
        let data = await init(element)
        tables.push(data);
    }
    return tables;
}

async function init(table){
    let data = new Table();
    data.sourceTableName =  table.TABLE_NAME;  
    data.tableComment =  table.TABLE_COMMENT;
    data.fields =  await getAllFields(table.TABLE_NAME);  
    for (const i in data.fields) {
        const element = data.fields[i];
        if(element.COLUMN_KEY=="PRI"){
            data.prikey = element;
            break;
        }
    }    
    return data;
}

function transformStr3(str){
    var re=/_(\w)/g;
    return str.replace(re,function ($0,$1){
        return $1.toUpperCase();
    });
}
/*
{
    TABLE_SCHEMA: 库名
    TABLE_NAME 表名
    COLUMN_NAME 列名
    DATA_TYPE 类型
    COLUMN_COMMENT 注释
}
*/
async function getAllFields(table) {  
    let rsdata = await getFidlds(table);
    let rs = [];    
    rs = rsdata.map(item=>{   
        Object.defineProperty(item,"colType",{
           get: function(){
               let fType = this.DATA_TYPE;             
               
               let msg = fieldGet(fType,this.COLUMN_NAME,this.COLUMN_COMMENT);
                if(msg.hasOwnProperty("maps")){
                    return msg; 
                }else{
                    return msg;
                }
           } 
        })
        
        Object.defineProperty(item,"javaType",{
            get: function(){
                if(this.DATA_TYPE=='tinyint'){
                    return "int"
                }
                if(this.DATA_TYPE=='varchar'){
                    return "String"
                }
                if(this.DATA_TYPE=='decimal'){
                    return "Double"
                }
                
                return this.DATA_TYPE;
            } 
         }) 

         Object.defineProperty(item,"javaFname",{
            get: function(){                        
                var strArr= this.COLUMN_NAME.split('_');
                for(var i=1;i<strArr.length;i++){
                    strArr[i]=strArr[i].charAt(0).toUpperCase()+strArr[i].substring(1);
                }
                return strArr.join('');                   
                return this.COLUMN_NAME;
            } 
         }) 

         Object.defineProperty(item,"javaUFname",{
            get: function(){                        
                var strArr= this.COLUMN_NAME.split('_');
                for(var i=0;i<strArr.length;i++){
                    strArr[i]=strArr[i].charAt(0).toUpperCase()+strArr[i].substring(1);
                }
                return strArr.join('');                   
                return this.COLUMN_NAME;
            } 
         }) 

        if(item.COLUMN_COMMENT==null||item.COLUMN_COMMENT.length<=1){           
            item.COLUMN_COMMENT = item.COLUMN_NAME;
            return item;
        } else{
            return item;
        } 
    })
    return  rs;
}

function Table(){   
    return {
        tableComment:"",
        set sourceTableName (value){
            this.tableName = transformStr3(value);  
            this.UpTableName = this.tableName.substring(0,1).toUpperCase()+this.tableName.substring(1,this.tableName.length);  
            this.sTableName = value;
        },
        get tableComment (){
            if(this._tableComment==null||this._tableComment.length<=1){
                return this.tableName;
            }else{
                return this._tableComment;
            }
        },
        set tableComment (value){
            this._tableComment = value;
        } 
    }      
}
